// 实现这个项目的构建任务
// 页面数据
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
const sass = require('sass');
const loadGruntTasks = require('load-grunt-tasks');
const mozjpeg = require('imagemin-mozjpeg');

module.exports = grunt => {
  grunt.initConfig({
    sass: {
      options: {
        sourceMap: true,
        implementation: sass
      },
      main: {
        files: {
          'dist/assets/styles/main.css': 'src/assets/styles/main.scss'
        }
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']
      },
      main: {
        files: {
          'dist/assets/scripts/main.js': 'src/assets/scripts/main.js'
        }
      }
    },
    imagemin: {
      options: {
        optimizationLevel: 3, // 优化级别
        use: [mozjpeg()]
      },
      dynamic: {
        files: [{
          expand: true,
          src: ['src/assets/images/*.{png,jpg,gif,svg}'],
          dest: 'dist/assets/images/'
        }]
      }
    },
    swigtemplates: {
      options: {
        defaultContext: {
          ...data
        },
        templatesDir: 'src/'
      },
      production: {
        dest: 'dist/',
        src: ['src/*.html']
      },
    },
    // 这个不知道怎么提取出来的是空的？？？？
    useref: {
      html: 'dist/*.html',
      temp: 'dist/'
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['bable']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      },
    }
  })

  // 自动加载grunt插件，不需要在引入
  loadGruntTasks(grunt)

  grunt.registerTask('default', ['sass', 'babel', 'imagemin', 'swigtemplates','useref', 'concat','watch'])
}
